package com.qqt.csr.message.event;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: yang hao
 * @Date: 2024/12/18 9:08
 * @Description: 事件发布策略工厂
 **/
@Component
public class EventPublisherStrategyFactory {

    /**
     * 策略集
     */
    private final Map<Class<?>, EventPublisherStrategy> strategies;

    /**
     * 构造器
     * @param strategies {@link List<EventPublisherStrategy>} 策略集
     */
    @Autowired
    public EventPublisherStrategyFactory(List<EventPublisherStrategy> strategies) {
        this.strategies = strategies.stream()
                .collect(Collectors.toMap(
                        EventPublisherStrategy::getSourceType,
                        strategy -> strategy
                ));
    }

    /**
     * 获取策略
     * @param source {@link Object} 事件源
     * @return {@link EventPublisherStrategy} 策略
     */
    public EventPublisherStrategy getStrategy(Object source) {
        return strategies.get(source.getClass());
    }

}
